package com.javaxiaobang.es.csdn.proxy;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * Created by xiaobang on 2020/5/29.
 */
@Slf4j
public class CsdnTest {


    //本地博客地址文本中的文章数量
    private static int blogUrlSize = 0;
    //本地博客地址文本装入HashMap中
    private static HashMap<Integer, String> LocalBlogUrl = null;
    //本地博客地址访问统计
    private static HashMap<Integer, Integer> LocalBlogUrlCount = null;
    //访问总量统计
    private static int count = 0;

    public static void main(String[] args) throws Exception {
//        String url = "http://tpv.daxiangdaili.com/ip/?tid=556533347000998&num=50";
//        for (int i = 0; i <100 ; i++) {
//            Document doc = Jsoup.connect(url)
////                    .userAgent("Mozilla")
////                    .cookie("auth", "token")
//                    .timeout(5000)
//                    .get();
//            System.out.println(doc);
//            System.setProperty("http.maxRedirects", "50");
//            System.getProperties().setProperty("proxySet", "true");
//            System.getProperties().setProperty("http.proxyHost", "188.186.56.1");
//            System.getProperties().setProperty("http.proxyPort", "8080");
//            System.clearProperty("http.maxRedirects");
//            System.getProperties().clear();
//        }

        run();
    }

    public static void run() {
        LocalBlogUrl = getLocalBlogUrl();
        blogUrlSize = LocalBlogUrl.size();
        try {
            while (true) {
                log.info("开始新的一轮刷新ip");
                String url = "http://tpv.daxiangdaili.com/ip/?tid=556533347000998&num=50";
                List<MyIp> ipList = getIp(url);
                LocalBlogUrlCount = initLocalBlogUrlCount();
                for (final MyIp myIp : ipList) {
                    System.setProperty("http.maxRedirects", "50");
                    System.getProperties().setProperty("proxySet", "true");
                    System.getProperties().setProperty("http.proxyHost", myIp.getAddress());
                    System.getProperties().setProperty("http.proxyPort", myIp.getPort());
                    int num = randomBlogUrl();
                    for (int t = 0; t <= num; t++) {
                        int id = randomBlogUrl();
                        try {
                            Document doc = Jsoup.connect(LocalBlogUrl.get(id))
                                    .userAgent("Mozilla")
                                    .cookie("auth", "token")
                                    .timeout(1000)
                                    .get();
                            if (doc != null) {
                                count++;
                                LocalBlogUrlCount.put(id, LocalBlogUrlCount.get(id) + 1);
//                                log.info("ID： " + id + "\tAddress： " + (LocalBlogUrl.get(id) + "\t成功刷新次数: " + count + "\t") + "Proxy： " + myIp.toString() + "\t");
                            }


                            sleepThread(randomClick());
//                        show();
                        } catch (Exception ee) {
                            log.warn(myIp.getAddress() + ":" + myIp.getPort() + "报错");
                        }
                    }
                }
                log.info("本轮刷新次数:{}", count);
            }
        } catch (Exception e) {
            log.info("本轮刷新次数:{}", count, e);
        }
    }

    //访问文章的随机函数，用来模拟真实的访问量操作，以免所有的文章访问量都是一样的，很明显是刷的，此操作随机访问文章，制造访问假象
    public static int randomBlogUrl() {
        if (blogUrlSize <= 0) {
            blogUrlSize = 1;
        }
        int id = new Random().nextInt(blogUrlSize);
        return id;
    }

    //时间的随机函数，用来模拟真实的访问量操作，以防被博客后台识别，模拟操作60-200秒内的随机秒数,
    public static int randomClick() {
        int time = (new Random().nextInt(5));
        return time;
    }

    //获取在【大象代理】中购买的IP，装入ArrayList<MyIp>中
    public static List<MyIp> getIp(String url) {
        List<MyIp> ipList = new ArrayList<MyIp>();
        try {
            if (System.getProperty("http.maxRedirects") != null) {
                System.clearProperty("http.maxRedirects");
            }

            System.getProperties().remove("proxySet");
            System.getProperties().remove("http.proxyHost");
            System.getProperties().remove("http.proxyPort");

            //1.向ip代理地址发起get请求，拿到代理的ip
            Document doc = Jsoup.connect(url)
                    .userAgent("Mozilla")
                    .cookie("auth", "token")
                    .timeout(5000)
                    .get();

            //2,将得到的ip地址解析除字符串
            String ipStr = doc.body().text().trim().toString();
            log.info("代理ip:{}", ipStr);

            //3.用正则表达式去切割所有的ip
            String[] ips = ipStr.split("\\s+");


            //4.循环遍历得到的ip字符串，封装成MyIp的bean
            for (final String ip : ips) {
                MyIp myIp = new MyIp();
                String[] temp = ip.split(":");
                myIp.setAddress(temp[0].trim());
                myIp.setPort(temp[1].trim());
                ipList.add(myIp);
            }
        } catch (Exception e) {
            log.warn("加载文档出错", e);
        }
        return ipList;
    }

    //获取本地BlogUrl.txt文本中的博客地址，并装入hashMap中，key=Integer,value=博客地址
    public static HashMap<Integer, String> getLocalBlogUrl() {
        HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
        BufferedReader br = null;
        try {
            File filePath = new File("/root/javaxiaobang/springcloud/csdn.txt");
            if (!filePath.exists()) {
                filePath = new File(System.getProperty("user.dir"), "csdn.txt");
            }
            FileReader in = new FileReader(filePath);
            br = new BufferedReader(in);
            String line = null;
            int id = 0;
            while ((line = br.readLine()) != null) {
                hashMap.put(id++, line);
            }
        } catch (Exception e) {

        } finally {
            try {
                br.close();
                br = null;
            } catch (Exception e) {

            }
        }
        return hashMap;
    }

    //休眠进程，单位是分钟,CSDN的规则好像是：每个IP访问一个博客地址的时间间隔是5-15分钟，计数一次
    public static void sleepThread(int s) throws InterruptedException {
        long ms = s * 10;
        Thread.sleep(ms);
    }

    //访问统计
    public static HashMap<Integer, Integer> initLocalBlogUrlCount() {
        HashMap<Integer, Integer> temp = new HashMap<Integer, Integer>();
        for (int i = 0; i < blogUrlSize; i++) {
            temp.put(i, 0);
        }
        return temp;
    }

    //展示访问统计总量
    public static void show() {
        log.info("访问量统计：");
        for (int i = 0; i < LocalBlogUrlCount.size(); i++) {
            log.info("博客【" + i + "】:" + LocalBlogUrlCount.get(i) + "次\t");
        }
        log.info("总计：" + count + "次");
    }

}
